package com.youhong.teethcare.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class Tools {
    public static boolean isChoice = false;
    public static boolean isChanged = false;
    public static int requestCode_setplan = 0;
    private static final int SHORTENED_LOCAL_NAME = 0x08;
    private static final int COMPLETE_LOCAL_NAME = 0x09;
    public static int PHONE = 0;
    public static boolean NeedMove = false;
    private static int DEVICE_J064 = 0;
    private static int DEVICE_J055 = 1;
    private static int DEVICE_J080 = 3;
    private static int DEVICE_J087 = 4;
    private static int DEVICE_B018 = 5;
    private static int DEVICE_B005 = 2;
    public static int SMS = 1;
    private static byte[] temp;

    public static String byte2Hex(byte[] data) {
        if (data != null && data.length > 0) {
            StringBuilder sb = new StringBuilder(data.length);
            for (byte tmp : data) {
                sb.append(String.format("%02X ", tmp));
            }
            return sb.toString();
        }
        return "no data";
    }

    public static List<UUID> parseUuids(byte[] advertisedData) {
        List<UUID> uuids = new ArrayList<UUID>();
        ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(
                ByteOrder.LITTLE_ENDIAN);
        while (buffer.remaining() > 2) {
            byte length = buffer.get();
            if (length == 0)
                break;

            byte type = buffer.get();
            switch (type) {
                case 0x02: // Partial list of 16-bit UUIDs
                case 0x03: // Complete list of 16-bit UUIDs
                    while (length >= 2) {
                        uuids.add(UUID.fromString(String.format(
                                "%08x-0000-1000-8000-00805f9b34fb",
                                buffer.getShort())));
                        length -= 2;
                    }
                    break;

                case 0x06: // Partial list of 128-bit UUIDs
                case 0x07: // Complete list of 128-bit UUIDs
                    while (length >= 16) {
                        long lsb = buffer.getLong();
                        long msb = buffer.getLong();
                        uuids.add(new UUID(msb, lsb));
                        length -= 16;
                    }
                    break;

                default:
                    buffer.position(buffer.position() + length - 1);
                    break;
            }
        }

        return uuids;
    }

    public static int getBcd(String value) {
        Integer m = Integer.parseInt(value, 16);
        return m.intValue();
    }

    public static String getWeekText(String day, String[] weeks) {
        StringBuffer sb = new StringBuffer();
        if (day.contains("0")) {
            sb.append(weeks[0]).append(" ");
        }
        if (day.contains("1")) {
            sb.append(weeks[1]).append(" ");
        }
        if (day.contains("2")) {
            sb.append(weeks[2]).append(" ");
        }
        if (day.contains("3")) {
            sb.append(weeks[3]).append(" ");
        }
        if (day.contains("4")) {
            sb.append(weeks[4]).append(" ");
        }
        if (day.contains("5")) {
            sb.append(weeks[5]).append(" ");
        }
        if (day.contains("6")) {
            sb.append(weeks[6]).append(" ");
        }
        return sb.toString();
    }


    public static String getPickerTime(int saveHour, int saveMin) {
        String hour = "";
        String min = "";
        if (saveHour < 10) {
            hour = "0" + saveHour;
        } else {
            hour = String.valueOf(saveHour);
        }
        if (saveMin < 10) {
            min = "0" + saveMin;

        } else {
            min = String.valueOf(saveMin);
        }
        return hour + ":" + min;
    }

    public static byte[] getNumberValue(String number) {
        if (TextUtils.isEmpty(number)) return new byte[11];
        int len = number.length();
        byte[] value = new byte[len];

        for (int i = 0; i < len; i++) {
            value[i] = (byte) number.charAt(i);
        }

        return value;
    }

    public static byte[] getNameValue(String name) {
        byte[] b = null;
        try {
            b = name.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return b;
    }

    public static boolean isChinese(char a) {
        int v = (int) a;
        return (v >= 19968 && v <= 171941);
    }

    public static boolean containsChinese(String s) {
        if (null == s || "".equals(s.trim()))
            return false;
        for (int i = 0; i < s.length(); i++) {
            if (isChinese(s.charAt(i)))
                return true;
        }
        return false;
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00时00分";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00时" + unitFormat(minute) + "分";
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + "时" + unitFormat(minute) + "分";
            }
        }
        return timeStr;
    }

    public static String sec2Time(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00:" + unitFormat(minute);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static BigDecimal getBigDecimal(float data, int mode) {
        BigDecimal bd = new BigDecimal(data);
        BigDecimal setScale = bd.setScale(mode, RoundingMode.HALF_UP);
        return setScale;
    }

    public static String decodeDeviceName(byte[] data) {
        String name = null;
        int fieldLength, fieldName;
        int packetLength = data.length;
        for (int index = 0; index < packetLength; index++) {
            fieldLength = data[index];
            if (fieldLength == 0)
                break;
            fieldName = data[++index];

            if (fieldName == COMPLETE_LOCAL_NAME
                    || fieldName == SHORTENED_LOCAL_NAME) {
                name = decodeLocalName(data, index + 1, fieldLength - 1);
                break;
            }
            index += fieldLength - 1;
        }
        return name;
    }

    /**
     * Decodes the local name
     */
    public static String decodeLocalName(final byte[] data, final int start,
                                         final int length) {
        try {
            return new String(data, start, length, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            Log.e("scan", "Unable to convert the complete local name to UTF-8",
                    e);
            return null;
        } catch (final IndexOutOfBoundsException e) {
            Log.e("scan", "Error when reading complete local name", e);
            return null;
        }
    }

    public static String getVersionByName(String deviceName) {
        String version = "";
        if (TextUtils.isEmpty(deviceName)) return version;
        /*if(deviceName.contains("+")){
            version="1638";
        }
        if(deviceName.contains("plus")){
            version="1668-01";
        }*/
        if (deviceName.contains("1751")) {
            version = "1751";
        }
        return version;
    }
}
